package com.weijianhuawen.wjblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weijianhuawen.wjblog.common.domain.dos.ArticleCategoryRelDo;
import com.weijianhuawen.wjblog.common.domain.dos.CategoryDo;

import java.sql.Wrapper;
import java.util.List;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description: 文章分类关联表mapper
 * @date: 2024-10-24 16:38
 */
public interface ArticleCategoryRelMapper extends BaseMapper<ArticleCategoryRelDo> {
    // 根据文章id删除对应的关联分类记录
    default int deleteArticleCategoryOfArticleByArticleId(Long articleId) {
        return delete(Wrappers.<ArticleCategoryRelDo>lambdaQuery()
                .eq(ArticleCategoryRelDo::getArticleId, articleId));
    }
    // 根据文章id查询对应分类id
    default ArticleCategoryRelDo selectByArticleId(Long articleId) {
        return selectOne(Wrappers.<ArticleCategoryRelDo>lambdaQuery()
                .eq(ArticleCategoryRelDo::getArticleId, articleId));
    }

    // 根据文章id更新
    default int updateByArticleId(ArticleCategoryRelDo articleCategoryRelDo) {
        return update(articleCategoryRelDo, Wrappers.<ArticleCategoryRelDo>lambdaQuery()
                .eq(ArticleCategoryRelDo::getArticleId, articleCategoryRelDo.getArticleId()));
    }

    // 根据分类id查询文章分类关联
    default ArticleCategoryRelDo selectOneByCategoryId(Long categoryId) {
        return selectOne(Wrappers.<ArticleCategoryRelDo>lambdaQuery()
                .eq(ArticleCategoryRelDo::getCategoryId, categoryId)
                .last("LIMIT 1"));
    }

    // 根据分类id查询所有关联记录
    default List<ArticleCategoryRelDo> selectListByCategory(Long categoryId) {
        return selectList(Wrappers.<ArticleCategoryRelDo>lambdaQuery()
                .eq(ArticleCategoryRelDo::getCategoryId, categoryId));
    }

    default List<ArticleCategoryRelDo> selectByArticleIds(List<Long> articleIds) {
        return selectList(Wrappers.<ArticleCategoryRelDo>lambdaQuery()
                .in(ArticleCategoryRelDo::getArticleId, articleIds));
    }
}
